package com.vip.base.redis;

import com.vip.base.BaseApplication;
import com.vip.base.service.ISysUserService;
import com.vip.common.response.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * description
 *
 * @author wgb
 * @date 2020/6/13 13:58
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BaseApplication.class)
public class RedisTestMain {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 模拟2000并发场景
     */
    @Test
    public void getUserDetailsTest() {
        long start = System.currentTimeMillis();
        // J.U.C 并发教程 --- 栅栏机制
        CyclicBarrier cyclicBarrier = new CyclicBarrier(200);
        for (int i = 0; i < 2000; i++) {
            new Thread(() -> {
                try {
                    RestTemplate restTemplate = new RestTemplate();
                    RestResponse result = restTemplate.getForObject("http://192.168.25.172:11000/vip-base-service/api/user/0ebec4d76ada9d54f44c731360f06125", RestResponse.class);
                    System.out.println(result.getData().toString());
                    // 等待，栅栏拦住所有线程，等待栅栏开启，2000线程同时执行这段代码
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));
    }

}
